/**
* @ProjectName: ShopSytem
* @Title: AdminLogin.java 
* @Package com.element5th.shop.servlet.admin 
* @Description: 管理员登录验证
* @author 第五元素小组   
* @date 2012-10-26 16:09:36 
* @version V1.0   
*/
package com.element5th.shop.servlet.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.element5th.shop.comm.BusinessException;
import com.element5th.shop.comm.DaoFactory;
import com.element5th.shop.comm.SystemException;
import com.element5th.shop.dao.admin.IAdminDao;
import com.element5th.shop.dto.BaseDto;

/**
 * Servlet implementation class AdminLogin
 */
@WebServlet("/page/admin/login")
public class AdminLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String adminName = request.getParameter("adminName");
		String adminPwd = request.getParameter("adminPwd");
		String txtCode = request.getParameter("txtcode");
		String codeCheck = (String) request.getSession().getAttribute("rand");
		
		String msg = null;
		String path = null;
		
		if(txtCode.equalsIgnoreCase(codeCheck)){
			BaseDto dto = new BaseDto();
			dto.getAdmin().setAdminName(adminName);
			dto.getAdmin().setAdminPwd(adminPwd);
			IAdminDao dao = DaoFactory.getAdminDao();
			
			try {
				dto = dao.adminLogin(dto);
				request.getSession().setAttribute("loginDto", dto);
				path = "/page/admin/page/home.jsp";
			} catch (SystemException e) {
				e.printStackTrace();
			} catch (BusinessException e) {
				msg = e.getId() + "：" + e.getMessage();
				path = "/page/admin/login.jsp";
			}
			
		} else {
			msg = "验证码不正确";
			path = "/page/admin/login.jsp";
		}
		
		request.setAttribute("msg", msg);
		
		this.getServletContext().getRequestDispatcher(path).forward(request, response);
	}

}
